﻿using MySql.Data.EntityFramework;
using ProjectDataBase;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Data.Entity.Migrations.Model.UpdateDatabaseOperation;

namespace ProjectDataBase
{
    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class ProjectDbContext : DbContext
    {
        public static string databaseString = "TT_HLSBYD";
        //在 MyDbContext 的构造函数体执行之前，DbContext 已经用这个连接字符串进行了初始化。这样，MyDbContext 实例可以立即使用配置好的数据库连接进行数据操作。
        public ProjectDbContext() : base(EFHelper.GetEntityMySqlConnectionString(databaseString))
        {
            Database.SetInitializer<DbContext>(new MigrateDatabaseToLatestVersion<DbContext, Migrations.Configuration>());
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // 配置模型
            //移除约定
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//（移除此约定将不会改变DbSet对应的表名）如果在Map里没有明确指定表名，将使用DbSet实体集的名称当作表名并且会强制转会为复数格式
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();//移除级联删除
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        }

        public virtual DbSet<TestData> TestDatas { get; set; }
    }
}
